<?php

namespace app\admin\service;

use app\admin\model\BoxTaskConfig;
use DateTime;

class BoxTaskConfigService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxTaskConfig();
    }

    public function getList(): array
    {
        $param = request()->param();
        $status = $param['status'] ?? '';
        $map = [];

        if (!empty($param['time_range'][0])) {
            $map[] = ['start_time', '<=', $param['time_range'][0]];
            $map[] = ['end_time', '>=', $param['time_range'][0]];
            $today = $param['time_range'][0];
        } else {
            $today = date('Y-m-d H:i:s');
        }
        if (isset($param['device']) && !empty($param['device'])) {
            $map[] = ['device', 'like', '%'.$param['device'].'%'];
        }
        /**
         * 1.待上架, 到时间点然后没有上架的游戏
         * 2.已上架
         * 3.已下架
         */
        switch ($status) {
            case 1:
                $map[] = ['status', '=', 2];
                $map[] = ['start_time', '<=', $today];
                $map[] = ['end_time', '>=', $today];
                break;
            case 2:
                $map[] = ['status', '=', 1];
                break;
            case 3:
                $map[] = ['status', '=', 2];
                break;
            default:
        }

        $boxArticleList = $this->model
            ->where($map)
            ->order('id desc');

        $list = $boxArticleList->paginate($param['limit']);
        if (empty($list)) {
            return message('暂无数据!', false);
        }
        $list = $list->toArray();

        //返回结果
        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list['data'],
            "count" => $list['total'],
        );
    }


    //添加编辑
    public function edit()
    {
        $param = request()->param();
        $param['start_time'] = $param['time_range'][0] ?? '';
        $param['end_time'] = $param['time_range'][1] ?? '';
        $param['create_time'] = date('Y-m-d H:i:s');
        $device = $param['device'] ?? '';
        $deviceArr = [];
        if(!empty($device)){
            $deviceArr = explode(',', $device);
        }
        foreach ($deviceArr as $v){
            $flag = $param['type_flag'] ?? 0;
            if (intval($flag) > 0) {
                $res = $this->model
                    ->where('type_flag', '=', $param['type_flag'])
                    ->where('device', 'like', '%'.$v.'%')
                    ->field('id')->find();
                if(isset($param['id'])){ //修改
                    if(!empty($res) && $param['id'] != $res['id']){
                        return message('当前任务已配置或已经上架相应平台,请选择其他具体任务选项或不同平台!', false);
                    }
                }else{  //添加
                    if (!empty($res)) return message('当前任务已配置或已经上架相应平台,请选择其他具体任务选项或不同平台!', false);
                }

            }
        }
        return parent::edit($param); // TODO: Change the autogenerated stub
    }


    public function status()
    {
        return parent::status(); // TODO: Change the autogenerated stub
    }

}